let _placeSearch: any;

type POI = {
  id: string;
  address: string;
  longitude: number;
  latitude: number;
};

export const placeSearch = (keyword: string): Promise<POI[]> => {
  if (!_placeSearch) {
    _placeSearch = new (AMap as any).PlaceSearch({
      city: '全国',
    });
  }
  return new Promise((resolve, reject) => {
    _placeSearch.search(keyword, (status: string, result: any) => {
      if (status === 'complete') {
        resolve(
          (result.poiList.pois ?? []).map((p: any) => ({
            ...p,
            longitude: p.location.lng,
            latitude: p.location.lat,
          })),
        );
      } else {
        reject(result);
      }
    });
  });
};
